package com.own.business.pay.module.wechat.util;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.own.business.pay.module.wechat.property.WechatPayProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * WechatPayRequestUtil
 *
 * @author chenxueli
 * @date 2023-08-28 09:33:00
 */
@Slf4j
@Component
public class WechatPayRequestUtil {

    @Resource
    private WechatPayProperty wechatPayProperty;

    /**
     * post发送请求
     *
     * @param url  请求地址
     * @param body 请求体
     * @return 响应
     */
    public JSONObject post(String url, String body) {
        var authorization = WechatPaySignatureUtil.authorization("POST", url, body, wechatPayProperty.getMchId(), wechatPayProperty.getSerialNo());
        var response = HttpRequest.post(url)
                .contentType("application/json")
                .header("Accept", "application/json")
                .header("Authorization", authorization)
                .body(body)
                .execute();
        return JSON.parseObject(response.body());
    }


    /**
     * get发送请求
     *
     * @param url 请求地址
     * @return 响应
     */
    public JSONObject get(String url) {
        var authorization = WechatPaySignatureUtil.authorization("GET", url, "", wechatPayProperty.getMchId(), wechatPayProperty.getSerialNo());
        var response = HttpRequest.post(url)
                .contentType("application/json")
                .header("Accept", "application/json")
                .header("Authorization", authorization)
                .execute();
        return JSON.parseObject(response.body());
    }

}
